1660C - Get an Even String - CodeForces Solution


dp greedy strings

Please click on ads to support us..

Python Code:

dp = [0]*200000
for _ in range(int(input())):
    S = list(input())
    N = len(S)

    dp[0] = 1
    prev = [-1]*26
    prev[ord(S[0])-97] = 0

    for i in range(1, N):
        dp[i] = dp[i-1]+1
        if prev[ord(S[i])-97] != -1:
            j = prev[ord(S[i])-97]
            dp[i] = min(dp[i], (dp[j-1] if (j-1>=0) else 0)+i-j-1)
        prev[ord(S[i])-97] = i
    print(dp[N-1])

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define ll      long long int 
#define vt      vector
#define pb      push_back
#define all(x)  (x).begin(), (x).end()
#define lb      lower_bound
#define ub      upper_bound
typedef pair<int, int> pairs;
const ll N = 1e7 + 1;
const int M=1e9 + 7;
#define INF 1e18
// bool isPrime(ll n)
// {
// 	if (n == 1)
// 		return false;

// 	for (ll i = 2; i * i <= n; i++) {
// 		if (n % i == 0)
// 			return false;
// 	}
// 	return true;
// }
// ll factorial(ll n)
// {
//     return (n==1 || n==0) ? 1: n * factorial(n - 1);
// }
// string bin(ll num)
// {
//     string str;
//       while(num)
//       {
//       if(num & 1) 
//         str+='1';
//       else 
//         str+='0';
//       num>>=1; // Right Shift by 1 
//     }   
//       return str;
// }
// string db(int n)
// {
//     // Size of an integer is assumed to be 32 bits
//     string ans;
//     for (int i = 64; i >= 0; i--)
//     {
//         int k = n >> i;
//         if (k & 1)
//             ans += '1';
//         else
//             ans += '0';
//     }
//     return ans;
// }

// string rev(string x)
// {
// 	string ans=x;
// 	reverse(all(ans));
// 	return ans;
// }
// ll step(ll x, ll p) 
// {
// 	ll ans = 1;
// 	while(p) 
// 	{
// 		if(p&1)
// 			ans=ans*x%M;
// 		p/=2;
// 		x=x*x%M;
// 	}
// 	return ans;
// }
int main()
{
	int t=1;
	cin>>t;
	while(t--)
	{
		ll n,m=1,j=0,z,h,c=0,d=0,ans=0,mx=0,mn=0,sum=0,f=0,f1=0,k,l,r,x,y,y1,y2,x1,x2;
		string s;
		cin>>s;
		// ll a[n];
		// vt<ll> v,v1,v2;
		// for(ll i=0;i<n;i++)
		//     cin>>a[i];
		unordered_map<char,ll> pending;
        for(ll i=0;i<s.size();i++)
        {
            if(pending[s[i]])
            {
                ans+=(pending.size()-1);
                pending.clear();
            }
            else
            {
                pending[s[i]]=1;
            }
        }
        ans+=pending.size();
        
	    cout<<ans<< endl;
		
		
		
		
		
		
		
		
		

        
        // for(auto x:v)
        // 	cout<<x<<" ";
        // cout<<endl;
        // for(auto x:v2)
        // 	cout<<x<<" ";
        // cout<<endl;
        
		
		
		// if(c>d1)
		// 	cout<<"ALEX"<<endl;
		// else if(c<d1)
        //     cout<<"BOB"<<endl;
        // else
        // 	cout<<"EQUAL"<<endl;
           
	

	}         
   
}
    

		
		



		
		
			
			
		
	

		
		

		
		
		
		



Comments

Submit
0 Comments
More Questions

145. Binary Tree Postorder Traversal
94. Binary Tree Inorder Traversal
101. Symmetric Tree
77. Combinations
46. Permutations
226. Invert Binary Tree
112. Path Sum
1556A - A Variety of Operations
136. Single Number
169. Majority Element
119. Pascal's Triangle II
409. Longest Palindrome
1574A - Regular Bracket Sequences
1574B - Combinatorics Homework
1567A - Domino Disaster
1593A - Elections
1607A - Linear Keyboard
EQUALCOIN Equal Coins
XOREQN Xor Equation
MAKEPAL Weird Palindrome Making
HILLSEQ Hill Sequence
MAXBRIDGE Maximise the bridges
WLDRPL Wildcard Replacement
1221. Split a String in Balanced Strings
1002. Find Common Characters
1602A - Two Subsequences
1555A - PizzaForces
1607B - Odd Grasshopper
1084A - The Fair Nut and Elevator
1440B - Sum of Medians